
import pygame
from pygame.sprite import Sprite

#继承Sprite 方便后续的检测碰撞
class Bullet(Sprite):
    def __init__(self,ai_settings,screen,ship):
        super().__init__()
        self.screen = screen
        self.rect = pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
        #设置子弹的初始位置
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top
        #移动中子弹的位置
        self.y = float(self.rect.y)
        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor

    def update(self):
        #子弹向上运行 只改变y
        self.y = self.y-self.speed_factor
        self.rect.y = self.y

    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)